package com.league.zjxn.jetpack.repository

import com.league.zjxn.app.SPSConstance
import com.league.zjxn.base.BaseManager
import com.league.zjxn.entity.*
import com.league.zjxn.utils.JsonUtils
import com.module.lemlin.http.CustomException
import com.module.lemlin.http.HttpResponseData
import rxhttp.RxHttp
import rxhttp.await

open class UserRepository : BaseManager() {

    suspend fun userContactUs(): ContactUsResponse =
        RxHttp.get("/api/contactAs")
            .addHeader("token", SPSConstance.token ?: "")
            .await()


    suspend fun userFollowStatistics(): FollowStatisticsResponse =
        RxHttp.get("/api/user/followStatistics")
            .addHeader("token", SPSConstance.token ?: "")
            .await()


    suspend fun userInfo(): UserInfoResponse =
        RxHttp.get("/api/user/getUserInfo")
            .addHeader("token", SPSConstance.token ?: "")
            .await()


//    suspend fun userInfo(): UserInfoResponse {
//
//        val responseData = RxHttp.get("/api/user/getUserInfo")
//            .addHeader("token", SPSConstance.token ?: "")
//            .await<ResponseData>()
//        if (responseData.code == 2000) {
//            val userInfo = try {
//                JsonUtils.objectToClass(responseData.data, UserInfo::class.java)
//            } catch (e: Exception) {
//                throw Exception()
//            }
//            return UserInfoResponse(data = userInfo).apply {
//                msg = responseData.msg
//                code = responseData.code
//            }
//        }
//        throw CustomException(errorCode = responseData.code.toString(), message = responseData.msg)
//    }


    suspend fun userFollowEnterprise(
        type: String,
        page: String,
        limit: String
    ): FollowEnterpriseResponse =
        RxHttp.get("/api/user/followEnterprise")
            .addHeader("token", SPSConstance.token ?: "")
            .add("type", type)
            .add("page", page)
            .add("limit", limit)
            .await()

    suspend fun userFollowProject(
        type: String,
        page: String,
        limit: String
    ): FollowProjectResponse =
        RxHttp.get("/api/user/followProject")
            .addHeader("token", SPSConstance.token ?: "")
            .add("type", type)
            .add("page", page)
            .add("limit", limit)
            .await()

    suspend fun userProjectApply(type: String, page: String, limit: String): ProjectApplyResponse =
        RxHttp.get("/api/user/getProjectListApp")
            .addHeader("token", SPSConstance.token ?: "")
            .add("time_type", type)
            .add("page", page)
            .add("limit", limit)
            .await()


    suspend fun userEnterpriseApprove(
        status: String? = null,
        page: String,
        limit: String
    ): EnterpriseApproveResponse {
        val http = RxHttp.get("/api/user/joinList")
            .addHeader("token", SPSConstance.token ?: "")
            .add("page", page)
            .add("limit", limit)
        if (!status.isNullOrEmpty()) {
            http.add("status", status)
        }
        return http.await()
    }

    suspend fun userApproveFlow(id: String): ApproveFlowResponse =
        RxHttp.get("/api/user/applyProgressList")
            .addHeader("token", SPSConstance.token ?: "")
            .add("id", id)
            .await()


    suspend fun userLeagueApprove(
        name: String, person: String, contract: String, industry_id: String? = null,
        province_id: String? = null, city_id: String? = null, content: String
    ): HttpResponseData {
        val http = RxHttp.postForm("/api/protocol/applyAlliance")
            .addHeader("token", SPSConstance.token ?: "")
            .add("name", name)
            .add("person", person)
            .add("contract", contract)
            .add("content", content)
        if (!industry_id.isNullOrEmpty()) {
            http.add("industry_id", industry_id)
        }
        if (!province_id.isNullOrEmpty()) {
            http.add("province_id", province_id)
        }
        if (!city_id.isNullOrEmpty()) {
            http.add("city_id", city_id)
        }
        return http.await()
    }


    suspend fun userIndustryList(): IndustryListResponse =
        RxHttp.get("/api/getIndustryList")
            .addHeader("token", SPSConstance.token ?: "")
            .await()


    suspend fun userRegionList(): RegionListResponse =
        RxHttp.get("/api/getArea")
            .addHeader("token", SPSConstance.token ?: "")
            .add("type", "1")
            .await()
}